﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace WpfEncryption
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += (s, e) =>
                          {
                              Title = string.Format("Encryption Ver {0} Copyright © 2012 Youngj ",
                                       System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
                          };
        }

        #region Form
        private void btnBrowser_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog { Title = "Open file" };
            if (dlg.ShowDialog() == true)
            {
                textEncrypt.Text = dlg.FileName;
            }
        }
        void SandAndFire(object sender, EventArgs e)
        {
            var windows = (sender as MainWindow);
            if (windows != null) GlassHelper.TryExtendGlassFrame(this, new Thickness(windows.MaxHeight));
        }

        private void btnGenerator_Click(object sender, RoutedEventArgs e)
        {
            var generator = new Hash_Algorithm.PasswordRandomGenerator();
            generator.DefineSets();
            textEncrypt.Text = generator.CreateRandomString(10, 15);
        }

        private void Window_Drop_1(object sender, DragEventArgs e)
        {
            GetFileDrop(e);
        }

       

        private void textEncrypt_Drop(object sender, DragEventArgs e)
        {
            GetFileDrop(e);
        }

        private void GetFileDrop(DragEventArgs e)
        {
            if (e.Data is System.Windows.DataObject && ((System.Windows.DataObject)e.Data).ContainsFileDropList())
            {
                var filename = ((DataObject)e.Data).GetFileDropList()[0];
                //MessageBox.Show("" + filename);
                textEncrypt.Text = filename;
            }
        }
        #endregion

        #region BackgroundWork
        private void DoWork(TextBox textBox, TypeEnc typeEnc)
        {
            textBox.Text = "computing...";
            var filename = textEncrypt.Text;
            var worker = new BackgroundWorker();
            var anotherWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
            AnotherWorkerProcess(anotherWorker, textBox);
            worker.DoWork += (s1, e1) => { e1.Result = filename.CheckSum(typeEnc); };

            worker.RunWorkerCompleted += (s1, e1) => { anotherWorker.CancelAsync(); SetText(textBox, (string)e1.Result); };
            anotherWorker.RunWorkerAsync();
            worker.RunWorkerAsync();

        }

        private void AnotherWorkerProcess(BackgroundWorker worker, TextBox textBox)
        {
            worker.DoWork += (s1, e1) =>
            {
                for (var i = 1; ; i++)
                {
                    if (worker.CancellationPending)
                    {
                        e1.Cancel = true;
                        return;
                    }
                    worker.ReportProgress(i);
                    Thread.Sleep(1000);
                }
            };
            worker.ProgressChanged += (s1, e1) => { textBox.Text = string.Format("computing {0}s", e1.ProgressPercentage); };
        }

        private void SetText(object sender, string text)
        {
            var textBox = sender as TextBox;
            if (textBox == null) return;

            var isUpper = chb.IsChecked ?? false;
            textBox.Text = isUpper ? text.ToUpper() : text;
        }
        #endregion

        #region Button CheckSum
        private void btnMD5_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textMD5, TypeEnc.MD5);
        }

        private void btnSHA1_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textSHA1, TypeEnc.SHA1);
        }

        private void btnCRC32_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textCRC32, TypeEnc.CRC32);
        }

        private void btnSHA256_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textSHA256, TypeEnc.SHA256);
        }

        private void btnSHA384_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textSHA384, TypeEnc.SHA384);
        }

        private void btnSHA1MD5_Click(object sender, RoutedEventArgs e)
        {
            DoWork(textEncode, TypeEnc.SHA1MD5);
        }
        #endregion






    }
}
